home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / chown.c < prev    next >
C/C++ Source or Header  |  1994-03-29  |  5KB  |  207 lines

  1. RCS_ID_C="$Id: chown.c,v 3.1 1994/03/29 12:56:35 ppessi Exp $";
  2. /*
  3.  * chown.c --- chown() for usergroup link library
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP User Library.
  8.  *
  9.  * Copyright © 1993,1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
  10.  *             Helsinki University of Technology, Finland.
  11.  *             All rights reserved.
  12.  *
  13.  * This function is based on SetOwner37 code
  14.  * Copyright © 1993 by Geert Uytterhoeven
  15.  * 
  16.  * Created      : Wed Sep 15 02:20:20 1993 ppessi
  17.  * Last modified: Mon Mar 28 02:06:33 1994 ppessi
  18.  *
  19.  */
  20.  
  21. /****** net.lib/chown *********************************************************
  22.  
  23.     NAME
  24.         chown - change owner and group of a file
  25.  
  26.     SYNOPSIS
  27.         #include <unistd.h>
  28.  
  29.         success = chown(path, owner, group)
  30.  
  31.         int chown(const char *, uid_t, gid_t);
  32.  
  33.     DESCRIPTION
  34.         The owner ID and group ID of the file named by path or referenced by
  35.         fd is changed as specified by the arguments owner and group.  The
  36.         owner of a file may change the group to a group of which he or she is
  37.         a member, but the change owner capability is restricted to the
  38.         super-user.
  39.  
  40.         Chown() clears the set-user-id and set-group-id bits on the file to
  41.         prevent accidental or mischievious creation of set-user-id and
  42.         set-group-id programs.
  43.  
  44.         One of the owner or group id's may be left unchanged by specifying it
  45.         as -1.
  46.  
  47.         If the final component of path is a symbolic link, the ownership and
  48.         group of the symbolic link is changed, not the ownership and group of
  49.         the file or directory to which it points.
  50.  
  51.     RETURN VALUES
  52.         Zero is returned if the operation was successful; -1 is returned if an
  53.         error occurs, with a more specific error code being placed in the
  54.         global variable errno.
  55.  
  56.     ERRORS
  57.         Chown() will fail and the file will be unchanged if:
  58.  
  59.         [ENOTDIR]     A component of the path prefix is not a directory.
  60.  
  61.         [EINVAL]      The pathname contains a character with the high-order
  62.                       bit set.
  63.  
  64.         [ENAMETOOLONG]
  65.                       A component of a pathname exceeded 80 characters, or an
  66.                       entire path name exceeded 1023 characters.
  67.  
  68.         [ENOENT]      The named file does not exist.
  69.  
  70.         [EACCES]      Search permission is denied for a component of the path
  71.                       prefix.
  72.  
  73.         [ELOOP]       Too many symbolic links were encountered in translating
  74.                       the pathname.
  75.  
  76.         [EPERM]       The effective user ID is not the super-user.
  77.  
  78.         [EROFS]       The named file resides on a read-only file system.
  79.  
  80.         [EFAULT]      Path points outside the process's allocated address
  81.                       space.
  82.  
  83.         [EIO]         An I/O error occurred while reading from or writing to
  84.                       the file system.
  85.  
  86.     SEE ALSO
  87.         chmod(2)
  88.  
  89. *****************************************************************************
  90. */
  91.  
  92. #include <libraries/usergroup.h>
  93. #include <proto/dos.h>
  94. #include <dos/dosextens.h>
  95.  
  96. #include "netlib.h"
  97.  
  98. #ifndef ACTION_SET_OWNER    
  99. #define ACTION_SET_OWNER    1036
  100. #endif
  101.  
  102. int chown(const char *name, uid_t uid, gid_t gid)
  103. {
  104.   BPTR lock;
  105.   short rc = -1;
  106.  
  107.   if (lock = Lock(name, ACCESS_READ)) {
  108.     if (uid == -1 || gid == -1) {
  109.       /* XXX We are supposed to do stat() and find out the suitable value */
  110.       
  111.     }
  112.     rc = DoPkt(((struct FileLock *)BADDR(lock))->fl_Task,
  113.            ACTION_SET_OWNER, 
  114.            NULL, lock, MKBADDR("\0"), 
  115.            (UG2MU(uid) << 16) | UG2MU(gid), NULL);
  116.     UnLock(lock);
  117.   }
  118.  
  119.   if (!rc) {
  120.     set_errno(IoErr());
  121.     return -1;
  122.   } else {
  123.     return 0;
  124.   }
  125. }
  126.  
  127. #ifdef DEBUGGING
  128. #include <proto/usergroup.h>
  129. #include <stdlib.h>
  130. #include <string.h>
  131. #include <exec/execbase.h>
  132. int errno;
  133. extern struct ExecBase *SysBase;
  134.  
  135. void PrintUserFault(LONG code, const UBYTE *banner);
  136.  
  137. const static char usage[] = "usage: chown [-fR] owner[:group] file ...";
  138.  
  139. void main(int argc, char *argv[])
  140. {
  141.   struct Process *p = (struct Process *)SysBase->ThisTask;
  142.   BPTR Stderr = p->pr_CES ? p->pr_CES : p->pr_COS;
  143.  
  144.   short perrors = 1, recursive = 0;
  145.   uid_t uid; gid_t gid = -1;
  146.   char *group, *user;
  147.  
  148.   while (argc > 1 && argv[1][0] == '-') {
  149.     switch (argv[1][1]) {
  150.     case 'f':
  151.       perrors = 0;
  152.       break;
  153.     case 'R':
  154.       recursive = 1;
  155.       break;
  156.     default:
  157.       FPrintf(Stderr, usage);
  158.       exit(10);
  159.     }
  160.     argv++, argc--;
  161.   }
  162.  
  163.   if (argc <= 2) {
  164.     FPrintf(Stderr, usage);
  165.     exit(10);
  166.   }
  167.  
  168.   user = argv[1]; argv++, argc--;
  169.   group = rindex(user, ':');
  170.  
  171.   if (group) {
  172.     *group++ = '\0';
  173.     if (StrToLong(group, &gid) < 1) {
  174.       struct group *gr = getgrnam(group);
  175.       if (gr) {
  176.     gid = gr->gr_gid;
  177.       } else {
  178.     if (perrors)
  179.       PrintUserFault(errno, "chown: getgrnam");
  180.     exit(RETURN_ERROR);
  181.       }
  182.     }
  183.   }
  184.  
  185.   if (StrToLong(user, &uid) < 1) {
  186.     struct passwd *pw = getpwnam(user);
  187.     if (pw) {
  188.       uid = pw->pw_uid;
  189.     } else {
  190.       if (perrors)
  191.     PrintUserFault(errno, "chown: getpwnam");
  192.       exit(RETURN_ERROR);
  193.     }
  194.   }
  195.  
  196.   while (argc-- > 1) {
  197.     if (chown(argv++[1], uid, gid) == -1) {
  198.       if (perrors)
  199.     PrintUserFault(errno, "chmod");
  200.       exit(RETURN_ERROR);
  201.     }
  202.   }
  203.  
  204.   exit(0);
  205. }
  206. #endif
  207.